<html xmlns:xhtml="http://www.w3.org/1999/xhtml" xmlns:xforms="http://www.w3.org/2002/xforms" xmlns:ev="http://www.w3.org/2001/xml-events" xmlns:xs="http://www.w3.org/2001/XMLSchema"
	xmlns="http://www.w3.org/1999/xhtml" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:xxforms="http://orbeon.org/oxf/xml/xforms" xmlns:p="http://www.orbeon.com/oxf/pipeline"
	xmlns:xi="http://www.w3.org/2001/XInclude" xmlns:exist="http://exist.sourceforge.net/NS/exist" xmlns:fr="http://orbeon.org/oxf/xml/form-runner" xmlns:nuds="http://nomisma.org/nuds"
	xmlns:xlink="http://www.w3.org/1999/xlink">
	<xhtml:head>
		<title>Numishare: Ingest Objects From Mantis</title>
		<!-- Core + Skin CSS -->
		<xhtml:link rel="stylesheet" type="text/css" href="/apps/numishare/css/jquery-ui-1.8.10.custom.css"/>
		<xhtml:link rel="stylesheet" href="/apps/numishare/css/reset-fonts-grids.css"/>
		<xhtml:link rel="stylesheet" href="/apps/numishare/css/base-min.css"/>
		<xhtml:link rel="stylesheet" href="/apps/numishare/css/fonts-min.css"/>

		<!-- numishare styling -->
		<xhtml:link rel="stylesheet" href="/apps/numishare/css/xforms.css"/>

		<xhtml:script type="text/javascript" src="/apps/numishare/javascript/jquery-1.6.1.min.js"/>
		<xhtml:script type="text/javascript" src="/apps/numishare/javascript/numishare-menu.js"/>
		<xforms:model>
			<!-- exist URL is stored in an XML file -->
			<xforms:instance id="exist-url">
				<xi:include href="exist-url.xml"/>
			</xforms:instance>

			<xforms:instance id="control-instance">
				<controls xmlns="">
					<collection-name/>
				</controls>
			</xforms:instance>

			<xforms:instance id="dump">
				<dump/>
			</xforms:instance>

			<!-- collections/authentication instances -->
			<xforms:instance id="collections-list">
				<collections xmlns="">
					<collection roll="admin" name="numishare"/>
				</collections>
			</xforms:instance>

			<xforms:instance id="config">
				<config xmlns=""/>
			</xforms:instance>

			<xforms:instance id="status">
				<status/>
			</xforms:instance>

			<xforms:instance id="identifier">
				<id xmlns=""/>
			</xforms:instance>

			<xforms:instance id="object">
				<nuds xmlns=""/>
			</xforms:instance>

			<!-- eXist queries -->
			<xforms:instance id="eXist-xquery">
				<exist:query xmlns:exist="http://exist.sourceforge.net/NS/exist">
					<exist:text>&lt;report xmlns:nuds="http://nomisma.org/id/nuds"> { for $foo in collection() return &lt;item> { $foo//*[local-name()='nudsHeader']/*[local-name()='nudsid'] } &lt;/item> } &lt;/report></exist:text>
				</exist:query>
			</xforms:instance>

			<xforms:instance id="collection-result">
				<exist:result/>
			</xforms:instance>

			<xforms:instance id="atom-feed">
				<feed xmlns="http://www.w3.org/2005/Atom"/>
			</xforms:instance>

			<xforms:instance id="associatedObject-template">
				<associatedObject xlink:href="" xmlns=""/>
			</xforms:instance>

			<!-- submissions -->
			<xforms:submission id="load-collections" serialization="none" method="get" action="{instance('exist-url')}collections-list.xml" replace="instance" instance="collections-list"
				xxforms:username="admin" xxforms:password="">
				<!-- if the config loads successfully, set the collection names based on authentication -->
				<xforms:action ev:event="xforms-submit-done">
					<!-- set default if security is false -->
					<xforms:action if="not(string(instance('control-instance')/request-security/role))">
						<xforms:setvalue ref="instance('control-instance')/collection-name">numishare</xforms:setvalue>
					</xforms:action>
					<!-- if there is a security role, set the collection-name value if it is in the list, otherwise set new collection name -->
					<xforms:action if="string(instance('control-instance')/request-security/role)">
						<xforms:action if="string(instance('collections-list')/collection[@role=instance('control-instance')/request-security/role]/@name)">
							<xforms:setvalue ref="instance('control-instance')/collection-name"
								value="instance('collections-list')/collection[@role=instance('control-instance')/request-security/role]/@name"/>
						</xforms:action>
					</xforms:action>
					<xforms:send submission="load-config"/>
				</xforms:action>
				<!-- if the config has not been created (given that the URL in exist-url.xml is correct), create it -->
				<xforms:message ev:event="xforms-submit-error" level="modal">Unable to load Numishare collections list.</xforms:message>
			</xforms:submission>

			<xforms:submission id="load-config" serialization="none" method="get" action="{instance('exist-url')}{instance('control-instance')/collection-name}/config.xml" xxforms:username="admin"
				xxforms:password="" replace="instance" instance="config"/>

			<!-- load object temporarily -->
			<xforms:submission id="load-object" serialization="none" method="get" action="{instance('exist-url')}{instance('control-instance')/collection-name}/objects/{instance('identifier')}.xml"
				replace="instance" instance="object" xxforms:username="admin" xxforms:password="">
				<xforms:message ev:event="xforms-submit-error" level="modal">Cannot load object: <xforms:output ref="instance('identifier')"/></xforms:message>
				<xforms:action ev:event="xforms-submit-done">
					<xforms:action xxforms:iterate="instance('atom-feed')/atom:entry">
						<xxforms:variable name="href" select="atom:link[not(@rel)]/@href"/>
						<xforms:action if="not(instance('object')//nuds:associatedObject[@xlink:href=$href])">
							<xforms:setvalue ref="instance('associatedObject-template')/@xlink:href" value="$href"/>							
							<xforms:insert origin="instance('associatedObject-template')" context="instance('object')/nuds:digRep" nodeset="./child::node()[last()]"/>
						</xforms:action>
					</xforms:action>
					<xforms:send submission="save-object"/>
				</xforms:action>
			</xforms:submission>
			
			<xforms:submission id="save-object" ref="instance('object')" xxforms:username="admin" xxforms:password=""
				action="{instance('exist-url')}{instance('control-instance')/collection-name}/objects/{instance('object')/nuds:nudsHeader/nuds:nudsid}.xml" method="put" replace="none">
				<xforms:message ev:event="xforms-submit-error" level="modal">Save Error. Are all required inputs filled?</xforms:message>
			</xforms:submission>

			<!-- queries -->
			<xforms:submission id="xquery-collection" ref="instance('eXist-xquery')" action="{instance('exist-url')}{instance('control-instance')/collection-name}/objects" method="post"
				replace="instance" instance="collection-result" xxforms:username="admin" xxforms:password="">
				<xforms:setvalue ref="instance('control-instance')/error" ev:event="xforms-submit-error">Error querying eXist database.</xforms:setvalue>
				<xforms:action ev:event="xforms-submit-done" xxforms:iterate="instance('collection-result')//nuds:nudsid">
					<xxforms:variable name="id" select="."/>
					<xforms:setvalue ref="instance('identifier')" value="$id"/>
					<xforms:send submission="query-mantis"/>
				</xforms:action>
			</xforms:submission>

			<xforms:submission id="query-mantis" serialization="none" method="get" action="http://numismatics.org/search/feed/?q=reference_display:&#x022;{instance('identifier')}&#x022;"
				replace="instance" instance="atom-feed" xxforms:username="admin" xxforms:password="">
				<xforms:action ev:event="xforms-submit-done" if="count(instance('atom-feed')/atom:entry) &gt; 0">					
					<xforms:send submission="load-object"/>
				</xforms:action>
			</xforms:submission>

			<!-- ***************** ACTIONS *************** -->
			<xforms:action ev:event="xforms-model-construct-done">
				<xforms:insert context="instance('control-instance')" nodeset="collection-name" position="after"
					origin="xxforms:call-xpl('oxf:/apps/numishare/xpl/get-authentication.xpl', 'dump', instance('dump'), 'data')"/>
				<xforms:send submission="load-collections"/>
			</xforms:action>
		</xforms:model>

	</xhtml:head>

	<xhtml:body class="yui-skin-sam">
		<xhtml:div id="doc4" class="yui-t2 content ui-corner-all">
			<xxforms:variable name="display_path">../</xxforms:variable>
			<xi:include href="header.xml"/>
			<div id="bd">
				<xhtml:div id="form">
					<xforms:group ref="instance('status')/text()">
						<div class="success">
							<xforms:output ref="instance('status')"/>
						</div>
					</xforms:group>
					<p>
						<a href="../../pages/">&lt; Return</a>
					</p> <xforms:trigger appearance="minimal">
						<xforms:label>Ingest ANS objects</xforms:label>
						<xforms:action ev:event="DOMActivate">
							<xforms:send submission="xquery-collection"/>
							<xforms:setvalue ref="instance('status')" ev:event="xforms-submit-done">Process complete.</xforms:setvalue>
						</xforms:action>
					</xforms:trigger>
				</xhtml:div>
			</div>
			<!--<widget:xforms-instance-inspector id="orbeon-xforms-inspector" xmlns:widget="http://orbeon.org/oxf/xml/widget"/>-->
		</xhtml:div>
	</xhtml:body>
</html>
